package com.cango.framework.base.interceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.cango.qutz.web.common.SessionUtils;
import com.cango.framework.base.Constants;
import com.cango.qutz.web.util.UtilHelper;
import com.cango.framework.base.Constants;
import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;

public class SessionInteceptor extends HandlerInterceptorAdapter {

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        if(null == SessionUtils.getSessionUser()) {
            //如果是ajax请求响应头会有，x-requested-with
            if (UtilHelper.notNull(request.getHeader("x-requested-with"))
                    && request.getHeader("x-requested-with").equalsIgnoreCase("XMLHttpRequest")) {
                //在响应头设置session状态
                response.setHeader("sessionstatus", "timeout");
                return true;
            } else {
                response.sendRedirect(request.getContextPath()+"/logout"+ Constants.SUFFIX_MVC_INTECEPTED);
                return false;
            }
        }
        return true;
    }

}
